﻿using System;
using System.Collections.Generic;
using System.Collections;

namespace Gmantis.Controls.DataGrid
{
    public class DataGridSelectedItemsCollection<T> : IEnumerable<T>, IEnumerable
    {
        // Fields
        private Func<T, bool> _contains;
        private Func<int> _count;
        private Func<IEnumerable<T>> _enumerable;
        private Func<int, T> _indexer;

        // Methods
        public DataGridSelectedItemsCollection(Func<int, T> indexer, Func<int> count, Func<T, bool> contains, Func<IEnumerable<T>> enumerable)
        {
            this._indexer = indexer;
            this._count = count;
            this._contains = contains;
            this._enumerable = enumerable;
        }

        public bool Contains(T item)
        {
            if (this._contains != null)
            {
                return this._contains(item);
            }
            return false;
        }

        public IEnumerator<T> GetEnumerator()
        {
            if (this._enumerable != null)
            {
                IEnumerable<T> enumerable = this._enumerable();
                if (enumerable != null)
                {
                    return enumerable.GetEnumerator();
                }
            }
            return null;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        // Properties
        public int Count
        {
            get
            {
                if (this._count != null)
                {
                    return this._count();
                }
                return 0;
            }
        }

        public T this[int index]
        {
            get
            {
                if (this._indexer != null)
                {
                    return this._indexer(index);
                }
                return default(T);
            }
        }
    }
}
